#include "head.h"
#include "log.h"

int init_socket(int* socket_fd, char* ip, char* port){

    *socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(*socket_fd == -1){
        ERROR_LOG("Error code: %d scoket fialed.", ERROR1);
    }

    struct sockaddr_in sockaddr;
    sockaddr.sin_family = AF_INET;
    sockaddr.sin_addr.s_addr = inet_addr(ip);
    sockaddr.sin_port = htons(atoi(port));

    int reuse = 1;
    setsockopt(*socket_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));

    int ret = bind(*socket_fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr));
    if(ret == -1){
        ERROR_LOG("Error code: %d bind fialed.", ERROR1);
    }

    ret = listen(*socket_fd, 10);
    if(ret == -1){
        ERROR_LOG("Error code: %d listen fialed.", ERROR1);
    }


    return 0;
}
